from email.mime.text import MIMEText
from email.header import Header
from smtplib import SMTP_SSL
from utils import rootDir
    # #qq邮箱smtp服务器
    # host_server = 'smtp.qq.com'
    # #sender_qq为发件人的qq号码
    # sender_qq = '897604980@qq.com'
    # #pwd为qq邮箱的授权码
    # pwd = 'aeanpnelhpzwbaja' ## xh**********bdc
    # #发件人的邮箱
    # sender_qq_mail = '897604980@qq.com'
    # #收件人邮箱
    # receiver = '18850458953@163.com'
    #
    # #邮件的正文内容
    # mail_content = '你好，这是使用python登录qq邮箱发邮件的测试'
    # #邮件标题
    # mail_title = 'python 的邮件'
    #
    # #ssl登录
    # smtp = SMTP_SSL(host_server)
    # #set_debuglevel()是用来调试的。参数值为1表示开启调试模式，参数值为0关闭调试模式
    # # smtp.set_debuglevel(1)
    # smtp.ehlo(host_server)
    # smtp.login(sender_qq, pwd)
    #
    # msg = MIMEText(mail_content, "plain", 'utf-8')
    # msg["Subject"] = Header(mail_title, 'utf-8')
    # msg["From"] = sender_qq_mail
    # msg["To"] = receiver
    # smtp.sendmail(sender_qq_mail, receiver, msg.as_string())
    # smtp.quit()
import smtplib
import string
from smtplib import SMTP_SSL
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.header import Header
from email.mime.application import MIMEApplication # 用于添加附件


host_server = 'smtp.qq.com'  #qq邮箱smtp服务器
sender_qq = '897604980@qq.com' #发件人邮箱
pwd = 'aeanpnelhpzwbaja'
receiver = '18850458953@163.com'
mail_title = 'Python接口自动化测试报告' #邮件标题

#邮件正文内容
mail_content = "打开index.html使用浏览器查看测试报告详情"

msg = MIMEMultipart()
msg["Subject"] = Header(mail_title,'utf-8')
msg["From"] = sender_qq
msg["To"] = Header("Python接口自动化测试报告","utf-8")

msg.attach(MIMEText(mail_content,'html'))
attachment = MIMEApplication(open(rootDir+r'/report/index.html','r', encoding='utf-8').read())
attachment["Content-Type"] = 'application/octet-stream'
# 给附件重命名
basename = "test.xlsx"
attachment.add_header('Content-Dispositon','attachment',filename=('utf-8', '', basename))#注意：此处basename要转换为gbk编码，否则中文会有乱码。
msg.attach(attachment)


try:
    smtp = SMTP_SSL(host_server) # ssl登录连接到邮件服务器
    smtp.set_debuglevel(1) # 0是关闭，1是开启debug
    smtp.ehlo(host_server) # 跟服务器打招呼，告诉它我们准备连接，最好加上这行代码
    smtp.login(sender_qq,pwd)
    smtp.sendmail(sender_qq,receiver,msg.as_string())
    smtp.quit()
    print("邮件发送成功")
except smtplib.SMTPException:
    print("无法发送邮件")
